# 基础测试脚本 - 网络安全漏洞知识图谱项目
Write-Host "=== 网络安全漏洞知识图谱测试环境 ===" -ForegroundColor Blue

# 创建基础目录
Write-Host "创建测试目录..." -ForegroundColor Yellow
$dirs = @("tests\data\raw", "tests\data\mock_apis", "tests\logs", "tests\reports", "tests\config")
foreach ($dir in $dirs) {
    if (!(Test-Path $dir)) {
        New-Item -ItemType Directory -Path $dir -Force | Out-Null
        Write-Host "创建: $dir" -ForegroundColor Green
    }
}

# 生成JSON测试数据
Write-Host "生成测试数据..." -ForegroundColor Yellow
$testData = @{
    vulnerabilities = @(
        @{ cve_id = "CVE-2023-TEST-001"; title = "Test Vulnerability 1"; severity = "HIGH"; cvss_score = 8.5 },
        @{ cve_id = "CVE-2023-TEST-002"; title = "Test Vulnerability 2"; severity = "MEDIUM"; cvss_score = 6.8 }
    )
} | ConvertTo-Json -Depth 3

$testData | Out-File -FilePath "tests\data\mock_apis\test_data.json" -Encoding UTF8
Write-Host "JSON数据已生成" -ForegroundColor Green

# 生成CSV测试数据
$csvData = "cve_id,title,severity,cvss_score`nCVE-2023-TEST-001,Test Vulnerability 1,HIGH,8.5`nCVE-2023-TEST-002,Test Vulnerability 2,MEDIUM,6.8"
$csvData | Out-File -FilePath "tests\data\raw\test_vulnerabilities.csv" -Encoding UTF8
Write-Host "CSV数据已生成" -ForegroundColor Green

# 运行基础测试
Write-Host "运行基础测试..." -ForegroundColor Yellow

# 文件读写测试
$testFile = "tests\data\temp_test.txt"
"测试内容" | Out-File -FilePath $testFile -Encoding UTF8
$content = Get-Content $testFile -Encoding UTF8
Remove-Item $testFile -Force

if ($content -eq "测试内容") {
    Write-Host "文件读写测试: 通过" -ForegroundColor Green
} else {
    Write-Host "文件读写测试: 失败" -ForegroundColor Red
}

# JSON处理测试
$testJson = @{ "test" = "data" } | ConvertTo-Json
$parsed = $testJson | ConvertFrom-Json

if ($parsed.test -eq "data") {
    Write-Host "JSON处理测试: 通过" -ForegroundColor Green
} else {
    Write-Host "JSON处理测试: 失败" -ForegroundColor Red
}

# 目录结构测试
$allExists = $true
foreach ($dir in $dirs) {
    if (!(Test-Path $dir)) {
        $allExists = $false
        break
    }
}

if ($allExists) {
    Write-Host "目录结构测试: 通过" -ForegroundColor Green
} else {
    Write-Host "目录结构测试: 失败" -ForegroundColor Red
}

# 检查Python环境
Write-Host "检查Python环境..." -ForegroundColor Yellow
try {
    $pythonResult = python --version 2>&1
    if ($LASTEXITCODE -eq 0) {
        Write-Host "Python环境: 可用 ($pythonResult)" -ForegroundColor Green
    } else {
        Write-Host "Python环境: 不可用" -ForegroundColor Yellow
    }
} catch {
    Write-Host "Python环境: 不可用" -ForegroundColor Yellow
}

# 生成简单报告
Write-Host "生成测试报告..." -ForegroundColor Yellow
$reportTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$reportData = @{
    timestamp = $reportTime
    status = "completed"
    files_generated = @(
        "tests\data\mock_apis\test_data.json",
        "tests\data\raw\test_vulnerabilities.csv"
    )
} | ConvertTo-Json -Depth 2

$reportPath = "tests\reports\basic_test_report.json"
$reportData | Out-File -FilePath $reportPath -Encoding UTF8
Write-Host "报告已生成: $reportPath" -ForegroundColor Green

Write-Host ""
Write-Host "🎉 基础测试完成！" -ForegroundColor Green
Write-Host "📁 测试数据已生成在 tests\data\ 目录下" -ForegroundColor Cyan
Write-Host "📊 查看报告: $reportPath" -ForegroundColor Cyan
Write-Host "🌐 打开交互式指南: tests\quick_test_guide.html" -ForegroundColor Cyan
Write-Host ""
Write-Host "✅ 测试环境已准备就绪，可以开始开发网络安全漏洞知识图谱项目！" -ForegroundColor Blue 